package com.authine.cloudpivot.web.api.service.impl;



import com.authine.cloudpivot.web.api.dao.XZZPXQMapper;
import com.authine.cloudpivot.web.api.domain.Constants;
import com.authine.cloudpivot.web.api.service.XZZPXQService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class XZZPXQServiceImpl implements XZZPXQService {

    @Autowired
    private XZZPXQMapper xzzpxqMapper;

    @Autowired
    private ProjectTeamServiceImpl projectTeamServiceImpl;
    /**
     * @Description : 根据项目，大区获取执行经理
     * @param ： storeNum
     * @param ： subjectName
     * @param ： areas
     * @author : Liun
     * @date : 2020/4/24
     */
   @Override
   public String getExecutive(String subjectName, String storeNames,String type){
       String[]  storeNameLists=storeNames.split(",");
       List<String> storeNameList=new ArrayList<>();
       for(String storeName:storeNameLists){
           if(!storeNameList.contains(storeName)){
               storeNameList.add(storeName);
           }
       }
       List<String> persons=new ArrayList<>();
       //店检需要获取督导
       SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
       String nowTime= sf.format(new Date());
       if(Constants.SHOP_INSPECTION.equals(type)){
           persons=xzzpxqMapper.getMember(subjectName,storeNameList,nowTime);
       }else{
           persons=xzzpxqMapper.getExecutive(subjectName,storeNameList);
       }
       //转换返回前端格式
       String lastUsers=formatPerson(persons);
       return lastUsers;
   };



   /**
    * @Description :根据项目、省份，获取执行经理
    * @param ： subjectName
    * @param ： provinces
    * @author : Liun
    * @date : 2020/6/8
    */
    @Override
    public String getExecutiveOther(String subjectName, String provinces){
        //最终返回执行经理
        List<String> persons=new ArrayList<>();
        String[]  provinceLists=provinces.split(",");
        List<String> provinceList=new ArrayList<>();
        for(String province:provinceLists){
            if(!provinceList.contains(province)){
                provinceList.add(province);
            }
        }
        persons=xzzpxqMapper.getExecutiveOther(subjectName,provinceList);
        //转换返回前端格式
        String lastUsers=formatPerson(persons);
        return lastUsers;
    };



    /**
     * @Description : 转换返回到前端的格式
     * @param ： persons
     * @author : Liun
     * @date : 2020/6/8
     */
    public String  formatPerson(List<String> persons){
        List<String> personNew=new ArrayList<>();
        String personNow;
        for(String person:persons){
            personNow=person.replace("[","").replace("]","");
            personNew.add(personNow);
        }
        JSONArray json = JSONArray.fromObject(personNew);
        String users=json.toString();
        String lastUsers= projectTeamServiceImpl.createUser(users);
        return lastUsers;
    }





}



